package com.yzt.minemodule.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.entity.mine.wallet.OrderListBean;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.minemodule.R;
import com.yzt.minemodule.activity.OrderDetailActivity;
import com.yzt.minemodule.adapter.OrderAdapter;
import com.yzt.minemodule.vm.OrderViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


/**
 * 作者：bobwang
 * 创建时间：2023-11-16 22:34:10
 * 描述：
 */
public class OrderFragment extends BaseFragment implements OnRefreshListener {

    private OrderAdapter orderAdapter;

    private OrderViewModel findViewModel;

    private RefreshLayout mRefreshLayout;

    private int type;

    // 状态 [ 0：待支付，1：已支付，2：已关闭 ]
    public static OrderFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("extra_type", type);
        OrderFragment fragment = new OrderFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    public void installViews() {
        type = getArguments().getInt("extra_type", 0);
        findViewModel = new ViewModelProvider(this).get(OrderViewModel.class);
        RecyclerView recyclerView = findViewById(R.id.rv_discover);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        orderAdapter = new OrderAdapter();
        orderAdapter.showPay(type == 0);
        recyclerView.setAdapter(orderAdapter);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        addListener();

        findViewModel.getOrder(type, mCurrentPage, PAGE_SIZE);

        findViewModel.OrderListSuccessEvent.observe(this, list -> {
            if (list != null && list.size() > 0) {
                if (mCurrentPage == 1) {
                    orderAdapter.removeEmptyView();
                    orderAdapter.setList(list);
                } else {
                    orderAdapter.addData(list);
                }
                orderAdapter.getLoadMoreModule().loadMoreComplete();
            } else {
                if (mCurrentPage == 1) {
                    orderAdapter.setEmptyView(getEmptyView());
                }
                orderAdapter.getLoadMoreModule().loadMoreEnd();
            }
        });

        findViewModel.OrderListFinallyEndEvent.observe(this, flag -> {
            mRefreshLayout.finishRefresh();
        });
    }

    /**
     * 列表刷新 加载 点击事件
     */
    private void addListener() {
        mRefreshLayout.setOnRefreshListener(this);
        orderAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                mCurrentPage++;
                findViewModel.getOrder(type, mCurrentPage, PAGE_SIZE);
            }
        });
        orderAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (ViewUtil.isFastDoubleClick()) {
                    return;
                }
                OrderListBean userInfo = orderAdapter.getItem(position);
                OrderDetailActivity.startSelf(getActivity(), userInfo.getOrderNo());
            }
        });

        orderAdapter.addChildClickViewIds(R.id.img_pay);
        orderAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (ViewUtil.isFastDoubleClick()) {
                return;
            }
            if (view.getId() == R.id.img_pay) {
                // 支付
            }
        });
    }

    @Override
    public void netWorkRequest() {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        findViewModel.getOrder(type, mCurrentPage, PAGE_SIZE);
    }
}
